using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using NexCart.Web.Repositories;
using NexCart.Web.Models;

namespace NexCart.Web.Controllers {
	public class ProductsController : Controller {

		IRepository repository;

		public ProductsController()
			: this(new SqlRepository()) {
		}


		public ProductsController(IRepository repository) {
			this.repository = repository;
		}

		public ActionResult Index() {
			return View(repository.ProductRepository.Products);
		}

		//
		// GET: /Products/Details/5

		public ActionResult Details(int id) {
			return View();
		}

		//
		// GET: /Products/Create

		public ActionResult Create() {
			return View();
		}

		//
		// POST: /Products/Create

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(FormCollection collection) {
			Product product = new Product() { Active = true };
			
			try {
				if (!TryUpdateModel(product, new string[] { "Name", "Description", "Price", "ShippingCostPeritem", "Weight" }))
					return View(product);

				repository.ProductRepository.Save(product);
				return RedirectToAction("Index");
			} catch {
				return View();
			}
		}

		//
		// GET: /Products/Edit/5

		public ActionResult Edit(int id) {
			return View();
		}

		//
		// POST: /Products/Edit/5

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(int id, FormCollection collection) {
			try {
				// TODO: Add update logic here

				return RedirectToAction("Index");
			} catch {
				return View();
			}
		}
	}
}
